// 题目描述：
// 给定一个由 1（陆地）和 0（水）组成的矩阵，岛屿指的是由水平或垂直方向上相邻的陆地单元格组成的区域，且完全被水域单元格包围。孤岛是那些位于矩阵内部、所有单元格都不接触边缘的岛屿。
// 现在你需要将所有孤岛“沉没”，即将孤岛中的所有陆地单元格（1）转变为水域单元格（0）。

// 输入描述：
// 第一行包含两个整数 N, M，表示矩阵的行数和列数。
// 之后 N 行，每行包含 M 个数字，数字为 1 或者 0，表示岛屿的单元格。

// 输出描述
// 输出将孤岛“沉没”之后的岛屿矩阵。

// 输入：
// 4 5
// 1 1 0 0 0
// 1 1 0 0 0
// 0 0 1 0 0
// 0 0 0 1 1

// 输出：
// 1 1 0 0 0
// 1 1 0 0 0
// 0 0 0 0 0
// 0 0 0 1 1

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    line = await readline()
    let [m, n] = line.split(' ').map((i) => parseInt(i)),
        graph = new Array(m).fill(0).map(() => new Array(n).fill(0));
    // 初始化邻接矩阵
    for (let i = 0; i < m; i++) {
        line = await readline()
        let rowNums = line.split(' ').map((i) => parseInt(i))
        for (let j = 0; j < n; j++) {
            graph[i][j] = rowNums[j]
        }
    }

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
                if (graph[i][j] === 1) {
                    graph[i][j] = 2
                    dfs(graph, i, j)
                }
            }
        }
    }

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (graph[i][j] === 1) {
                graph[i][j] = 0
            }
            if (graph[i][j] === 2) {
                graph[i][j] = 1
            }
        }
    }
    console.log(graph)
}()

function dfs(graph, i, j) {
    let direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    for (let [left, right] of direction) {
        let newx = i + left,
            newy = j + right;
        if (newx < 0 || newx >= graph.length || newy < 0 || newy >= graph[0].length) continue
        if (graph[newx][newy] === 1) {
            graph[newx][newy] = 2
            dfs(graph, newx, newy)
        }
    }
}






void async function () {
    line = await readline()
    let [m, n] = line.split(' ').map((i) => parseInt(i)),
        graph = new Array(m).fill(0).map(() => new Array(n).fill(0));
    // 初始化邻接矩阵
    for (let i = 0; i < m; i++) {
        line = await readline()
        let rowNums = line.split(' ').map((i) => parseInt(i))
        for (let j = 0; j < n; j++) {
            graph[i][j] = rowNums[j]
        }
    }

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
                if (graph[i][j] === 1) {
                    graph[i][j] = 2
                    bfs(graph, i, j)
                }
            }
        }
    }

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (graph[i][j] === 1) {
                graph[i][j] = 0
            }
            if (graph[i][j] === 2) {
                graph[i][j] = 1
            }
        }
    }
    console.log(graph)
}()

function bfs(graph, i, j) {
    let direction = [[1, 0], [-1, 0], [0, 1], [0, -1]],
        queue = [[i, j]]
    while (queue.length > 0) {
        let [originI, originJ] = queue.pop()
        for (let [left, right] of direction) {
            let newx = originI + left,
                newy = originJ + right;
            if (newx < 0 || newx >= graph.length || newy < 0 || newy >= graph[0].length) continue
            if (graph[newx][newy] === 1) {
                graph[newx][newy] = 2
                queue.push([newx, newy])
            }
        }
    }
}
